package com.xnyzc.lhy.nio.mq;

import com.alibaba.fastjson.JSON;
import com.xnyzc.lhy.common.component.config.security.RocketConfig;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @program: bolt-servers
 * @description: mq生产者
 * @author: huxuekuo
 * @create: 2020-02-10 15:56
 **/
@Slf4j
@Component
public class MQProducer {
    /**
     * 生产者
     */
    @Autowired
    private DefaultMQProducer defaultMQProducer;

    @Autowired
    private RocketConfig rocketConfig;

    /**
     * 给滴滴同步(做数据同步的发送)
     */
    public void sendMsg(Object param, String tags) {
        log.info("进入发送消息方法(Start)");
        Message message = new Message(rocketConfig.getTopic(), tags, JSON.toJSONString(param).getBytes());
        try {
            SendResult send = defaultMQProducer.send(message);
            log.info("MQ发送消息 订单信息{},发送结果{}", param, send);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        log.info("发送消息结束(End)");
    }
}
